import { defineStore } from 'pinia'
interface IStore {
  userInfo: {
    username?: string
    id?: number
  }
  people: number[]
}
export const useUserStore = defineStore('user', {
  state: (): IStore => ({
    userInfo: {
      username: '',
      id: -1,
    },
    people: [2, 3],
  }),
  persist: true,
  getters: {
    // fullMessage: (state) => `The message is "${state.message}".`,
    // deleteUser: (state) => state.message,
  },
  actions: {
    // 异步更新 message
    // async updateMessage(newMessage: string): Promise<string> {
    //   return new Promise((resolve) => {
    //     setTimeout(() => {
    //       // 这里的 this 是当前的 Store 实例
    //       this.message = newMessage
    //       resolve('Async done.')
    //     }, 3000)
    //   })
    // },
    // 同步更新 message
    // updateMessageSync(newMessage: string): string {
    //   // 这里的 this 是当前的 Store 实例
    //   this.message = newMessage
    //   return 'Sync done.'
    // },
    saveUser(userInfo: { username: string; id: number }) {
      const username = userInfo.username
      const id = userInfo.id
      this.userInfo.username = username
      this.userInfo.id = id
      console.log(this.userInfo)
    },
    deleteUser() {
      this.userInfo = {
        username: '',
        id: -1,
      }
    },
  },
})
